home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / me_cd25.zip / MUTT2.ZIP / DIR.MUT < prev    next >
Text File  |  1992-11-09  |  4KB  |  144 lines

  1.   ;; dir.mut : directory stack stuff for UNIX versions of ME
  2.   ;; pgms defined:
  3.   ;; - cd   :  Change the current directory.
  4.   ;; - dirs :  Show the contents of the directory stack.  The top (or left
  5.   ;;     most) entry is the current directory, next is the directory you
  6.   ;;     left to get to the current directory.
  7.   ;; - pwd  :  Show the current directory.
  8.   ;; - pu   :  Change the current directory, using the stack to save the
  9.   ;;     changes.
  10.   ;;     (pu directory) pushes the current directory on the stack and sets
  11.   ;;       the current directory to directory.
  12.   ;;     (pu "") swaps the top 2 entries on the stack.
  13.   ;;     (pu +n) swaps the top of the stack with the nth stack item.  n
  14.   ;;       starts at zero.
  15.   ;; - po discards the top entry on the stack and sets the current
  16.   ;;     directory to the new top of stack.
  17.   ;; Requires:
  18.   ;;   popup.mut
  19.   ;; C Durland 10/87, 5/88    Public Domain
  20.  
  21. (include me2.h)
  22.  
  23. (const stack-size 10)    ; the max number of entries on the directory stack
  24.  
  25. (small-int num-stack-entries)
  26.  
  27. (defun
  28.   init-dirstack MAIN { (clear-stack) (push (sub~ (current-directory))) }
  29.   cd
  30.   {
  31.     (if (current-directory (complete CC_FNAME "change directory to: "))
  32.       {
  33.     (pop)    ;; nuke the first entry to make room for this one
  34.     (msg "moved to " (push (sub~ (current-directory))))
  35.     TRUE
  36.       }
  37.       { (msg "Not a valid directory") FALSE })
  38.   }
  39.   pwd { (msg "current directory: " (sub~ (current-directory))) }
  40.   pu        ;; push directory
  41.   {
  42.     (string name name2)
  43.     (int n)
  44.  
  45.     (name (complete CC_FNAME "pushd to: "))
  46.     (if (== name "")    ; swap top 2 items on stack
  47.     {
  48.       (if (< num-stack-entries 2)
  49.     { (msg "directory stack too small to swap") FALSE (done) })
  50.       (name (pop)) (name2 (pop))
  51.       (push name)  (push name2)
  52.  
  53.       (cd name2)
  54.       (done)
  55.     })
  56.  
  57.     (if (== (extract-element name 0) "+")   ; +n => move (n+1)th entry to top
  58.     {
  59.       (n (convert-to NUMBER (extract-elements name 1 10000)))
  60.       (if (>= n num-stack-entries)
  61.     { (msg "Only got " num-stack-entries " entries")(done) })
  62.       (name (popn n))
  63.       (push name)    ;; push a dummy that is nuked my (cd)
  64.  
  65.       (cd name)
  66.       (done)
  67.     })
  68.     (if (current-directory name)    ;; directory exists
  69.       {
  70.     (if (>= num-stack-entries stack-size)    ;; shrink stack
  71.         (popn (- num-stack-entries 1)))
  72.         (msg "moved to " (push (sub~ (current-directory))))
  73.     TRUE
  74.       }
  75.       { (msg "Not a valid directory") FALSE })
  76.   }
  77.   po        ;; pop directory
  78.   {
  79.     (if (< num-stack-entries 2) { (msg "directory stack empty") FALSE (done) })
  80.     (pop)
  81.     (cd (peek 0))
  82.   }
  83.   sub~ (string dir-name) HIDDEN    ; convert /users/craig/... to ~/...
  84.   {
  85.     (int n)
  86.  
  87.     (n (length-of (getenv "HOME")))
  88.     (if (and (!= n 0) (== (getenv "HOME") (extract-elements dir-name 0 n)))
  89.     (concat "~" (extract-elements dir-name n 1000))
  90.     dir-name
  91.     )
  92.   }
  93. )
  94.  
  95.  
  96. (defun
  97.   MAIN
  98.   {
  99.     (require "menu-box" "popup")        ;; for (dirs)
  100.   }
  101.   dirs
  102.   {
  103.     (int j)
  104.  
  105.     (menu-box
  106.       ">Directory Stack"
  107.       "-"
  108.       (for (j 0) (< j num-stack-entries)(+= j 1) (push-arg (peek j)))
  109.     )
  110.   }
  111. )
  112.  
  113.  
  114.  
  115. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  116. ;;;;;;;;;;;;; Stack Details ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  117. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  118.  
  119. (list dir-stack)
  120.  
  121. (defun
  122.   clear-stack HIDDEN
  123.   {
  124.     (while (!= 0 num-stack-entries) (pop))
  125.   }
  126.   push (string dir) HIDDEN        ;; returns name
  127.   {
  128.     (insert-object dir-stack -1 dir)
  129.     (num-stack-entries (length-of dir-stack))
  130.     dir
  131.   }
  132.   peek (int n) HIDDEN { (extract-element dir-stack n) }
  133.   popn (int n) HIDDEN
  134.   {
  135.     (string dir)
  136.  
  137.     (dir (extract-element dir-stack n))
  138.     (remove-elements dir-stack n 1)
  139.     (num-stack-entries (length-of dir-stack))
  140.     dir
  141.   }
  142.   pop HIDDEN { (popn 0) }
  143. )
  144.